package com.hpin.mlxteacher.tree;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.adapter.ChooseAdapter;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.bean.People;
import com.hpin.mlxteacher.widget.GridViewForScrollView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 选择宝宝页面
 * Created by Aaron on 15/7/30.
 */
public class ChooseBabyActivity extends BaseActivity {

    private ChooseAdapter chooseAdapter;
    private List<People> peoples;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_baby);

        initWidget();
    }

    private void initWidget() {

        TextView tv_left = (TextView) findViewById(R.id.tv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_left.setText(getResources().getString(R.string.select_all));
        tv_title.setText(getResources().getString(R.string.choose_baby));
        tv_right.setText(getResources().getString(R.string.done));

        GridViewForScrollView gv_baby = (GridViewForScrollView) findViewById(R.id.gv_baby);

        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        peoples = new ArrayList<People>();
        People p1 = new People();
        p1.name = "张大大";
        People p2 = new People();
        p2.name = "张小小";
        People p3 = new People();
        p3.name = "张大小";
        People p4 = new People();
        p4.name = "张小大";
        People p5 = new People();
        p5.name = "张轩轩";
        People p6 = new People();
        p6.name = "李大大";
        peoples.add(p1);
        peoples.add(p2);
        peoples.add(p3);
        peoples.add(p4);
        peoples.add(p5);
        peoples.add(p6);
        chooseAdapter = new ChooseAdapter(this, peoples);
        gv_baby.setAdapter(chooseAdapter);


        gv_baby.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //反选 收件人
//                ((People) chooseAdapter.getItem(position)).isSelect =  (((People) chooseAdapter.getItem(position)).isSelect) == 0 ? 1 : 0;
                ((People) chooseAdapter.getItem(position)).isSelect = !((People) chooseAdapter.getItem(position)).isSelect;

                chooseAdapter.notifyDataSetChanged();
            }
        });


    }


    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.tv_left://全选
                for (People p : peoples) {

                    p.isSelect = true;
                    chooseAdapter.notifyDataSetChanged();
                }

                break;
            case R.id.tv_right://完成

                List<People> chooseedPeoples=new ArrayList<People>();

                for (People p : peoples) {

                    if(p.isSelect) {
                        chooseedPeoples.add(p);
                    }
                }
                Intent data = new Intent();
                data.putExtra("children" , (Serializable)chooseedPeoples);
                setResult(2, data);
                finish();
                break;

        }

    }
}
